import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { hasPermission, isLoggedIn } from "@/assets/js/permission";
import { ElMessage } from "element-plus";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/portal",
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("../views/Register.vue"),
  },
  {
    path: "/portal",
    name: "Portal",
    component: () => import("../views/Portal.vue"),
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("../views/Home.vue"),
    children: [
      {
        path: "/equipment",
        name: "Equipment",
        component: () => import("../views/Equipment/EquipmentManage.vue"),
      },
      {
        path: "/equipment/details",
        name: "EquipmentDetails",
        component: () => import("../views/Equipment/EquipmentDetails.vue"),
      },
      {
        path: "/equipment/details-look",
        name: "EquipmentDetailsLook",
        component: () => import("../views/Equipment/EquipmentDetailsLook.vue"),
      },
      {
        path: "/equipment-booking",
        name: "EquipmentBooking",
        component: () => import("../views/EquipmentBooking/Index.vue"),
      },
      {
        path: "/equipment-booking-manage",
        name: "EquipmentBookingManage",
        component: () => import("../views/EquipmentBookingManage/Index.vue"),
      },
      {
        path: "/notify",
        name: "Notify",
        component: () => import("../views/Notify/Index.vue"),
        children: [
          {
            path: "/equipment-alarm",
            name: "EquipmentAlarm",
            component: () => import("../views/Notify/EquipmentAlarm.vue"),
          },
          {
            path: "/system-notify",
            name: "SystemNotify",
            component: () => import("../views/Notify/SystemNotify.vue"),
          },
        ],
      },
      {
        path: "/update-password",
        name: "UpdatePassword",
        component: () => import("../views/UpdatePassword.vue"),
      },
    ],
  },
  {
    path: "/system-manage",
    name: "SystemManage",
    component: () => import("../views/SystemManage/Index.vue"),
    children: [
      {
        path: "/user-manage",
        name: "UserManage",
        component: () => import("../views/SystemManage/UserManage.vue"),
      },
      {
        path: "/role-manage",
        name: "RoleManage",
        component: () => import("../views/SystemManage/RoleManage.vue"),
      },
      {
        path: "/oga-manage",
        name: "OgaManage",
        component: () => import("../views/SystemManage/OgaManage.vue"),
      },
      {
        path: "/equipment-types",
        name: "EquipmentTypes",
        component: () => import("../views/SystemManage/EquipmentTypes.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 不需要权限检查的路由
  const publicRoutes = ["Login", "Register"];

  if (publicRoutes.includes(to.name as string)) {
    next();
    return;
  }

  // 检查用户是否已登录
  if (!isLoggedIn()) {
    ElMessage.error("请先登录");
    next("/login");
    return;
  }

  // 检查用户是否有访问该路由的权限
  if (to.name && !hasPermission(to.name as string)) {
    ElMessage.error("您没有权限访问该页面");
    // 重定向到用户有权限访问的第一个页面
    const accessibleRoutes = ["Portal", "EquipmentBooking", "EquipmentAlarm"];
    for (const route of accessibleRoutes) {
      if (hasPermission(route)) {
        next({ name: route });
        return;
      }
    }
    // 如果没有可访问的路由，重定向到首页
    next({ name: "Portal" });
    return;
  }

  next();
});

export default router;
